package chapter3;

/**
 * Created by qpx on 2018/5/12.
 */
public class Person {
    public static void main(String[] args) {
        final Person p1 = new Person();
        final Person p2 = new Person();
        Thread sleepThread = new Thread(() ->{
            synchronized (p1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("sleepThread get p1's lock");
                synchronized (p2){
                    System.out.println("sleepThread get p2's lock");
                }
                System.out.println("sleepThread release p2's lock");
            }
            System.out.println("sleepThread release p1's lock");
        });
        Thread sleepThreadExt = new Thread(() -> {
            synchronized (p2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("sleepThreadExt get p2's lock");
                synchronized (p1){
                    System.out.println("sleepThreadExt get p1's lock");
                }
                System.out.println("sleepThreadExt release p1's lock");
            }
            System.out.println("sleepThreadExt release p2's lock");
        });
        sleepThread.start();
        sleepThreadExt.start();
    }
}
